package com.white.commonlib.base;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.white.commonlib.CommonError;
import com.white.commonlib.ApiException;
import com.white.commonlib.AppConfig;
import com.white.commonlib.crash.CrashHandler;
import com.white.commonlib.crash.MyDbHelper;
import com.white.commonlib.crash.UploadCrashLogService;
import com.white.commonlib.receiver.NetStateChangeReceiver;
import com.white.commonlib.utils.NetUtil;

/**
 * Created by yutf on 2017/8/17 0017.
 */

public class AppContext extends Application implements
        NetStateChangeReceiver.NetChangeListener,
        Application.ActivityLifecycleCallbacks
{
    public static Handler handler;
    protected static AppContext context;

    private AppConfig appConfig;
    private MyDbHelper dbHelper;

    @Override
    public void onCreate()
    {
        super.onCreate();
        context = this;
        handler = new Handler(Looper.getMainLooper());
        dbHelper = new MyDbHelper(context);
        registSomthing();
        registerActivityLifecycleCallbacks(this);
    }

    public MyDbHelper getDbHelper()
    {
        return dbHelper;
    }

    public void setAppConfig(final AppConfig appConfig)
    {
        this.appConfig = appConfig;
        if(appConfig.isCatchException())
        {
            CrashHandler.getInstance()
                    .init(AppContext.getContext(), appConfig.getShutdownListener());
        }
        startService(new Intent(this, UploadCrashLogService.class));
        new Thread()
        {
            @Override
            public void run()
            {
                appConfig.setNetworkAvilable(NetUtil.isNetworkConnected(context));
                appConfig.setNetType(NetUtil.GetNetworkType(context));
            }
        }.start();
    }

    public AppConfig getAppConfig()
    {
        if(appConfig == null)
            throw new ApiException(CommonError.INNER_NO_CONFIG_ERROR, "No AppConfig Setting");
        return appConfig;
    }

    public static Handler getHandler()
    {
        return handler;
    }

    public static AppContext getContext()
    {
        return context;
    }

    @Override
    public void onChanged(String netType, boolean isNetworkOk)
    {
        appConfig.setNetType(netType);
        appConfig.setNetworkAvilable(isNetworkOk);
    }

    private NetStateChangeReceiver ncr;
    //注册网络状态变化监听器
    private void registSomthing()
    {
        ncr = new NetStateChangeReceiver();
        ncr.setNetChangeListener(this);
        registerReceiver(ncr, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState)
    {
//        LogManager.e("listcycle   ->  onActivityCreated");
    }

    @Override
    public void onActivityStarted(Activity activity)
    {
//        LogManager.e("listcycle   ->  onActivityStarted");
    }

    @Override
    public void onActivityResumed(Activity activity)
    {
//        LogManager.e("listcycle   ->  onActivityResumed");
    }

    @Override
    public void onActivityPaused(Activity activity)
    {
//        LogManager.e("listcycle   ->  onActivityPaused");
    }

    @Override
    public void onActivityStopped(Activity activity)
    {
//        LogManager.e("listcycle   ->  onActivityStopped");
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState)
    {
//        LogManager.e("listcycle   ->  onActivitySaveInstanceState");
    }

    @Override
    public void onActivityDestroyed(Activity activity)
    {
//        LogManager.e("listcycle   ->  onActivityDestroyed");
    }
}
